package com.ff.ticket.repository;

import com.ff.ticket.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserRepository extends JpaRepository<User,String> {

    //根据id查找用户
    @Query(value = "SELECT * FROM user a  where a.id=?1", nativeQuery = true)
    public User findUserById(String id);

    //根据姓名查找用户
    @Query(value = "SELECT * FROM user a  where a.name=?1", nativeQuery = true)
    public User findUserByName(String name);

    //根据帐号查找用户
    @Query(value = "SELECT * FROM user a  where a.account=?1", nativeQuery = true)
    public User findUserByAccount(String account);

    //根据帐号查找用户
    @Query(value = "SELECT * FROM user a  where a.role=?1", nativeQuery = true)
    public List<User> findUsersByRole(String role);

    //根据帐号查找用户
    @Query(value = "SELECT * FROM user a  where a.role=:role and (a.id like CONCAT('%',:tag,'%') or a.account like CONCAT('%',:tag,'%') or a.name like CONCAT('%',:tag,'%') )", nativeQuery = true)
    public List<User> findUsersByGroupAndTag(@Param("role")String role, @Param("tag")String tag);

    //根据帐号查找用户
    @Query(value = "SELECT * FROM user a  where a.id like CONCAT('%',:tag,'%') or a.account like CONCAT('%',:tag,'%') or a.name like CONCAT('%',:tag,'%') ", nativeQuery = true)
    public List<User> findUsersByTag(@Param("tag")String tag);
}
